<?php
$table = $_GET ['table'];

if (isset ( $_FILES ['fichiersattachessample'] ) and $_FILES ['fichiersattachessample'] ['error'] == 0) {
	move_uploaded_file ( $_FILES ['fichiersattachessample'] ['tmp_name'], $_SESSION ['pathway'] . $_SESSION ['pathway_js'] . '/uploads/' . basename ( $_FILES ['fichiersattachessample'] ['name'] ) );
	chmod ( $_SESSION ['pathway'] . $_SESSION ['pathway_js'] . '/uploads/' . basename ( $_FILES ['fichiersattachessample'] ['name'] ), 0777 );
	$fichiersattaches = basename ( $_FILES ['fichiersattachessample'] ['name'] );
} else {
	if (isset ( $_POST ['fichiersattachessample'] ) and $_POST ['fichiersattachessample'] != '') {
		$fichiersattaches = $_POST ['fichiersattachessample'];
	}
}

foreach ( $_POST as $key => $value ) {
	if ($key != '') {
		$value = mysql_real_escape_string ( $value );
		// ~ $value = mysqli_real_escape_string($link,$value);
		$value = str_replace ( ',', '.', $value );
		$_POST [$key] = $value;
	}
}

if (isset ( $_POST ['dna'] )) {
	$dna = $_POST ['dna'];
} else {
	$dna = '';
}
if (isset ( $_POST ['sourcedna'] )) {
	$sourcedna = $_POST ['sourcedna'];
} else {
	$sourcedna = '';
}
if (isset ( $_POST ['wheresample'] )) {
	$wheresample = $_POST ['wheresample'];
} else {
	$wheresample = '';
	$wheresample = mysql_real_escape_string ( $wheresample );
}
if (isset ( $_POST ['whysample'] )) {
	$whysample = $_POST ['whysample'];
} else {
	$whysample = '';
	$whysample = mysql_real_escape_string ( $whysample );
}
$neurologistsample = strtoupper ( $neurologistsample );
if (isset ( $_POST ['dnasample1'] ) and $_POST ['dnasample1'] != 'other') {
	$dnasample = $_POST ['dnasample1'];
} elseif (isset ( $_POST ['dnasample2'] )) {
	$dnasample = $_POST ['dnasample2'];
} else {
	$dnasample = '';
}
$qualitysample = '';
if (isset ( $_POST ['sourcednasample'] )) {
	$sourcednasample = $_POST ['sourcednasample'];
} else {
	$sourcednasample = '';
}
if (isset ( $_POST ['qualitysample'] )) {
	foreach ( $_POST ['qualitysample'] as $qs ) {
		$qualitysample .= ' ' . $qs;
	}
}
if (isset ( $_POST ['concentrationsample'] )) {
	$concentrationsample = $_POST ['concentrationsample'];
} else {
	$concentrationsample = '';
}
if (isset ( $_POST ['quantitemicrolitre'] )) {
	$quantitemicrolitre = $_POST ['quantitemicrolitre'];
} else {
	$quantitemicrolitre = '';
}
if (isset ( $_POST ['quantitenanogramme'] )) {
	$quantitenanogramme = $_POST ['quantitenanogramme'];
} else {
	$quantitenanogramme = '';
}
if (isset ( $_POST ['datesample'] )) {
	$datesample = $_POST ['datesample'];
} else {
	$datesample = '';
}
if (isset ( $_POST ['usersample'] )) {
	$usersample = $_POST ['usersample'];
} else {
	$usersample = '';
}
if (isset ( $_POST ['todoprelevement'] )) {
	$todoprelevement = $_POST ['todoprelevement'];
} else {
	$todoprelevement = '';
}
if (isset ( $_POST ['testsgenetiques'] )) {
	$testsgenetiques = $_POST ['testsgenetiques'];
} else {
	$testsgenetiques = '';
	$testsgenetiques = mysql_real_escape_string ( $testsgenetiques );
}
if (isset ( $_POST ['fichiersattaches'] ) and $_POST ['fichiersattaches'] != '') {
	$fichiersattaches = $_POST ['fichiersattaches'];
} else {
	$fichiersattaches = '';
}

$array_tables = Array (
		'linkage',
		'screening',
		'wholegenomeanalysis' 
);
foreach ( $array_tables as $table ) {
	if (isset ( $_GET ['edit'] )) {
		$update = "UPDATE `$db`.`$table` SET 
		`index` = '$edit',
		`dna` = '$dna',
		`sourcedna` = '$sourcedna',
		`where` = '$wheresample',
		`why` = '$whysample',
		`code4` = '$dnasample1',
		`codeexterne` = '$sourcednasample',
		`centre` = '$qualitysample',
		`neurologist` = '$concentrationsample',
		`modelinkage` = '$quantitemicrolitre',
		`locusteste` = '$quantitenanogramme',
		`haplotype` = '$datesample',
		`linkage` = '$user',
		`lodscore` = '$todoprelevementtraitement1',
		`commentlinkage` = '$testsgenetiques',
		WHERE `$table`.`index` = $edit;";
		// echo $update."<br />";
		mysql_query ( mysql_real_escape_string($update) ) or die ( "Can't modify entry : " . mysql_error () );
		$_POST = array ();
	} else {
		$recupereindexmaxlinkage = "select MAX(`index`) FROM `$table`";
		$resultindex = mysql_query ( $recupereindexmaxlinkage ) or die ( "Impossible de r&eacute;cup&eacute;rer l'index de la table linkage" );
		while ( $ligne = mysql_fetch_row ( $resultindex ) ) {
			$indexlinkage = $ligne [0] + 1;
		}
		$insertlinkage = "INSERT INTO `$db`.`$table` (`index`, `code1`, `code2`, `code3`, `code4`, `codeexterne`, `centre`, `neurologist`, `modelinkage`, `locusteste`, `haplotype`, `linkage`, `lodscore`, `commentlinkage`, `date`, `user`, `fichiersattaches`, `diagnosticlinkage`, `afaire`, `validation`, `commentairesconclusion`) VALUES ('$indexlinkage','$code1', '$code2','$code3','$code4','$codeexternelinkage', '$centre', '$neurologist', '$modelinkage', '$locusteste', '$haplotype', '$linkage', '$lodscorelinkage', '$commentairelinkage', '$datelinkage', '$user', '$fichiersattaches', '$diagnosticlinkage', '$afaire', '$validation', '$commentairesconclusion');";
		mysql_query ( mysql_real_escape_string($insertlinkage) ) or die ( "Impossible d'ins&eacute;rer donn&eacute;es dans table linkage : " . mysql_error () );
		// echo $insertlinkage;
	}
}

?>
